package com.qf.listener;

import com.qf.entity.Goods;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author 刘亚康
 * @Date 2020/4/6
 * @Version 1.0'
 * @Description
 */
@Component
public class SolrQueueListener {
    @Autowired
    private SolrClient solrClient;

    @RabbitListener(queues = "solrQueue")
    public void receiveAddGoodsSolrQueueMsg(Goods goods){
        SolrInputDocument document=new SolrInputDocument();
        document.addField("goodsName",goods.getGoodsName());
        document.setField("goodsDesc",goods.getGoodsDesc());
        document.setField("id",goods.getGoodsId());
        document.setField("goodsPic",goods.getGoodsPic());
        document.setField("goodsPrice",goods.getGoodsPrice().toString());

        try {
            solrClient.add(document);
            solrClient.commit();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
